#include "../include/mtSerial.h"
#include "../include/rw_function_serial.h"
#include <time.h>
#include <omp.h>
 
int* rw_serial( const int NParticles, const int Size )
{
    int *mat;
    mat = (int*) calloc (Size * Size, sizeof(int) );    

    //Contole da poscicao da particula no dominio
    int x, y;
    int random_number, ok;
    
    //variaveis de contole
    int particles, step;
    int parar = Size*Size;
    //double start, end;
    
    /* Inicializando a semente do gerador de numeros pseudo aleatorios Serial Merssene Twister */
    init_genrand(time(NULL));
    
    //start = omp_get_wtime();
    for( particles = 0; particles < NParticles; particles++ ) 
    {
        /* colocando a particula no ponto inicial */
        x = y = 0;

        ++mat[0];
        step = 0;
                
        /*
         * iniciando a caminhada de uma particula
         * Parada por distancia percorrida ou por chegada em casa
         */
        
        while((step < parar) || ((x != Size - 1)&&(y != Size - 1)) )
        {
            random_number = genrand_int32() % 4;

            ok = 0;

            switch ( random_number ){
                case 0:
                    if ( ( x - 1 ) >= 0 ){
                        --x;
                        ok = 1;
                    }
                    break;
                case 1:
                    if ( ( y + 1 ) < Size ){
                        ++y;
                        ok = 1;
                    }
                    break;
                case 2:
                    if ( ( x + 1) < Size ){
                        ++x;
                        ok = 1;
                    }
                    break;
                case 3:
                    if ( (y -1) >= 0 ){
                        --y;
                        ok = 1;
                    }
                    break;
            }
            if(ok){
                step++;
                ++mat[(x * Size) + y];
            }
        }
    }
    //end = omp_get_wtime();
    //printf("Tempo Serial: %lf\nProbabilidade: %f\n", end-start, (mat[Size*Size-1] / (double)NParticles) );
    return mat;
}

